package com.wlient.basic.pojo;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

class Scratch {

    public static class Student {
        public String name;
        public Integer age;

        public BigDecimal year;

        public Student(String name, Integer age, BigDecimal year) {
            this.name = name;
            this.age = age;
            this.year = year;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public BigDecimal getYear() {
            return year;
        }

        public void setYear(BigDecimal year) {
            this.year = year;
        }

        public String customShow(Function<Student, String> fun) {
            return fun.apply(this);
        }
    }

    public static  <R,T> R toatlantic(Function<T, R> fun, List<T> list, Class<R> clazz) {
        R result = null;
        if (Integer.class.equals(clazz)) {
            List<R> collect = list.stream().map(fun).collect(Collectors.toList());
            Integer sum = 0;
            for (int i = 0; i < collect.size(); i++) {
                sum +=(Integer)collect.get(i);
            }
            result = (R)sum;
        } else if (BigDecimal.class.equals(clazz)) {
            BigDecimal sum = BigDecimal.ZERO;
            List<R> collect = list.stream().map(fun).collect(Collectors.toList());
            for (int i = 0; i < collect.size(); i++) {
                sum = sum.add((BigDecimal) collect.get(i));
            }
            result = (R) sum.setScale(2,BigDecimal.ROUND_HALF_UP);

        }
        return result;
    }


    public static void main(String[] args)  {
        ArrayList<Student> list = new ArrayList<>();
        list.add(new Student("ljw", 1, new BigDecimal(3.33)));
        list.add(new Student("ljw", 2, new BigDecimal(2.22)));
        list.add(new Student("ljw", 3, new BigDecimal(1.11)));


        Integer toatlantic = toatlantic(Student::getAge, list, Integer.class);
        System.out.println(toatlantic);

        BigDecimal toatlantic2 = toatlantic(Student::getYear, list, BigDecimal.class);
        System.out.println(toatlantic2);
//
//        //方式1
//        for (Student st : list) {
//            System.out.println(st.customShow(s -> s.name + ": " + s.name));
//        }
//
//
//        //方式2
//        Function<Student, String> styleOne = s -> {
//            String result = "Name:" + s.name + " and Age:" + s.age;
//            return result;
//        };
//        for (Student st : list) {
//            System.out.println(st.customShow(styleOne));
//        }
//        //方式3
//        Function<Student, String> styleTwo = s -> "Name:" + s.name + " and Age:" + s.age;
//        for (Student st : list) {
//            System.out.println(st.customShow(styleTwo));
//        }
    }
}